ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Well, I guess then you can still not provide some more representative sample data, right? I am still unclear what you mean by more general solution. Do you want to read the data from a file?
I participated in the thread that OP linked and if I remember correctly then OP is not allowed to install any 3rd party tools at all.
Well, I guess then you can still not provide some more representative sample data, right? I am still unclear what you mean by more general solution. Do you want to read the data from a file?
the confusion was whether i was specifically looking for a c.s.v. output for ls. i was just using it as an example therefore ls -m doesnt help. i needed something that puts commas in place of any white-space or new-lines between strings for any generic comnand from piped input (without me having to worry about preceding or trailing commas which mash up sql in() clause.)
BTW for robust functions I use printf not echo.
For example:
Code:
join "," -n
I always seem to forget about that. This problem has never backfired on and I have only encountered it when someone pointed it out. Maybe I should give more consideration to 'printf' in the future, but I guess bad habits die hard :)
I always prefer to use shell built-ins as much as possible. And you can do a lot with them.
In this case, since we're dealing with lists of filenames, the go-to tools are arrays and globbing (assuming bash or similar shell that supports them).
Code:
$ flist=( * ) # note that the sort order may differ from ls -1
$ clist=$( IFS=, ; echo "${flist[*]}" )
$ echo "$clist"
or:
$ printf -v clist '%s,' * #no need to bother with an array with printf
$ clist="${clist%,}"
$ echo "$clist"
For the first option, an array variable with * as its index expands to all of its elements, separated by the first character of IFS.
Unfortunately, due to the parsing sequence, you have to set IFS first as a separate command, before variable expansion. So in this case I have opted to expand the result in a subshell and transfer the output to a new variable, to avoid having the modified IFS affect the main shell.
The second option uses the printf idea from earlier, except that the superfluous comma is handled by again transferring the resulting string to a second variable, and using a simple parameter substitution to remove it.
edit1: Doh, I missed that MadeInGermany has already given this solution.
edit2: Just a heads-up, "join" is already the name of a coreutil application, albeit one with a completely different function.
Last edited by David the H.; 02-23-2020 at 08:57 AM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.